package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.AttractionImgs;
import com.qzct.entity.Attractions;
import com.qzct.entity.CuisineImgs;
import com.qzct.entity.po.AttractionSave;
import com.qzct.entity.po.CuisinesSave;
import com.qzct.entity.vo.AttractionVo;
import com.qzct.entity.vo.CuisineVo;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.CuisineImgsService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.Cuisines;
import com.qzct.service.CuisinesService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-11
 */
@RestController
@Api(tags = "美食")
@RequestMapping("/cuisines")
public class CuisinesController {

    @Autowired
    private CuisinesService cuisinesService;
    @Autowired
    private CuisineImgsService cuisineImgsService;

    /**
     * 添加。
     *
     * @param cuisines 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") Cuisines cuisines) {
        return cuisinesService.save(cuisines);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键删除")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return cuisinesService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param cuisines 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") Cuisines cuisines) {
        return cuisinesService.updateById(cuisines);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<Cuisines> list() {
        return cuisinesService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public ResultVo getInfo(@PathVariable @ApiParam("主键") int id) {
        CuisineVo cuisineVo = new CuisineVo();
        cuisineVo.setCuisines(cuisinesService.getById(id));
        cuisineVo.setList(cuisineImgsService.getByCid(id));
        return new ResultVo(cuisineVo);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<Cuisines> page(@ApiParam("分页信息") Page<Cuisines> page) {
        return cuisinesService.page(page);
    }


    /**
     * 添加。
     *
     * @param
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("saves")
    @ApiOperation("添加美食plus")
    public ResultVo save(@RequestBody @ApiParam("") CuisinesSave cuisinesSave) {
        cuisinesService.save(cuisinesSave.getCuisines());
        int id = cuisinesService.getMaxId();
        for (int i = 0; i < cuisinesSave.getUrls().size(); i++) {
            CuisineImgs cuisineImgs = new CuisineImgs();
            cuisineImgs.setUrl(cuisinesSave.getUrls().get(i));
            cuisineImgs.setCuisineId(id);
            cuisineImgsService.save(cuisineImgs);
        }
        return ResultVo.success("添加成功");
    }
    // 热门美食
    @GetMapping("hot")
    @ApiOperation("热门美食")
    public ArrayList<CuisineVo> hot(){
        ArrayList<CuisineVo> list = new ArrayList<>();
        ArrayList<Integer> hot = cuisinesService.getHot();
        for (int i = 0; i < hot.size(); i++) {
            CuisineVo cuisineVo = new CuisineVo();
            cuisineVo.setCuisines(cuisinesService.getById(hot.get(i)));
            cuisineVo.setList(cuisineImgsService.getByCid(hot.get(i)));
            list.add(cuisineVo);
        }
        return list;
    }
    @GetMapping("like/{id}")
    @ApiOperation("点赞")
    public ResultVo like(@PathVariable @ApiParam("主键") Serializable id) {
        cuisinesService.addlike(id);
        return ResultVo.success("成功");
    }
    @GetMapping("all")
    @ApiOperation("查看全部")
    public ResultVo all() {
        ArrayList<Cuisines> list = cuisinesService.getList();
        ArrayList<CuisineVo> vos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CuisineVo cuisineVo = new CuisineVo();
            cuisineVo.setCuisines(list.get(i));
            cuisineVo.setList(cuisineImgsService.getByCid(list.get(i).getCuisineId()));
            vos.add(cuisineVo);
        }
        return ResultVo.success("成功",vos);
    }
}
